using AppFiniteStateMachine;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using AppEventUtils;


namespace AppGameState
{
    public enum GameStateEnum
    {
        Init,
        Stop,
        Exit,
        
        Store,
        Battle,
        Win,
        Lose,
    }

    public class GameInitState : IState
    {
        private GameStateManager manager;
        private GameParams parameter;

        private float duration_time = 0f;
        
        public GameInitState(GameStateManager manager)
        {
            this.manager = manager;
            this.parameter = (GameParams)manager.parameter;
            
            // BdUtils.log("=== state parameter", BdUtils.conv_obj_to_json(manager.parameter));
        }
        
        public void OnEnter()
        {
            duration_time = 0f;
            
            parameter.store_objs.SetActive(false);
            parameter.battle_objs.SetActive(false);

            // BdPackage.BdUtils.log("GameInitState OnEnter");
        }

        public void OnUpdate()
        {
            while (!MyPointsManager.instance.is_ready)
                return;
            
            duration_time += Time.deltaTime;

            if (duration_time > parameter.max_time_for_init)
                manager.TransitionState(GameStateEnum.Store);
        }

        public void OnExit()
        {
            BdUtils.log("GameInitState OnExit");
        }
    }
    
    public class StoreState : IState
    {
        private GameStateManager manager;
        private GameParams parameter;

        private float duration_time = 0f;

        public StoreState(GameStateManager manager)
        {
            this.manager = manager;
            this.parameter = manager.parameter;
        }
        
        public void OnEnter()
        {
            BaseInfoManager.instance.current_rounds += 1;
            
            duration_time = 0f;

            parameter.store_objs.SetActive(true);

            BdPackage.BdUtils.log("StoreState OnEnter");

            if (GameController.instance.frozen_store_cards.Count > 0)
            {
                GameController.instance.store_soldiers.Clear();
                
                foreach (var frozen_store_card_i in GameController.instance.frozen_store_cards)
                {
                    GameController.instance.store_soldiers.Add(frozen_store_card_i);
                    var comp = frozen_store_card_i.GetComponent<CardController>();
                    comp.is_frozen = false;
                }
                GameController.instance.refresh_store_cards(refresh: false);

                MyPointsManager.instance.add_store_owner();
                
                GameController.instance.frozen_store_cards.Clear();
            }
            else
            {
                MyPointsManager.instance.add_store_owner();

                GameController.instance.refresh_store_cards();
            }
        }

        public void OnUpdate()
        {
            duration_time += Time.deltaTime;
            if (duration_time > parameter.max_time_for_store)
                manager.TransitionState(GameStateEnum.Battle);
        }

        public void OnExit()
        {
            parameter.store_objs.SetActive(false);

            GameController.instance.get_frozen_cards();
            
            // if (BaseInfoManager.instance.frozen_store_cards)
            // {
            //     BdPackage.BdUtils.log($"--- 111 frozen_store_cards: {BaseInfoManager.instance.frozen_store_cards}");
            //     GameController.instance.refresh_store_cards(refresh: false);
            //     BaseInfoManager.instance.frozen_store_cards = false;
            // }
            // else
            // {
            //     BdPackage.BdUtils.log($"--- 222 frozen_store_cards: {BaseInfoManager.instance.frozen_store_cards}");
            //     GameController.instance.refresh_store_cards();
            // }
            
            GameController.instance.hide_store_cards();
            
            BdPackage.BdUtils.log($"StoreState OnExit, num of frozen_cards: {GameController.instance.frozen_store_cards.Count}");
        }
    }
    public class BattleState : IState
    {
        private GameStateManager manager;
        private GameParams parameter;

        private float duration_time = 0f;

        private bool is_show_ending = false;
        
        public BattleState(GameStateManager manager)
        {
            this.manager = manager;
            this.parameter = manager.parameter;
        }
        
        public void OnEnter()
        {
            duration_time = 0f;
            is_show_ending = false;
            
            parameter.battle_objs.SetActive(true);
            BdPackage.BdUtils.log("BattleState OnEnter");

            GameController.instance.refresh_enemy_cards();

            GameController.instance.is_fighting = false;
            GameController.instance.start_fight();

            MyPointsManager.instance.add_store_owner(cancel: true);
            MyPointsManager.instance.add_enemy_hero(cancel: false);
            
            // GameController.instance.core_load_card_obj_by_id(current_enemy_id);
        }

        public void OnUpdate()
        {
            duration_time += Time.deltaTime;
            if (duration_time < 1f)
                return;
            
            if (!GameController.instance.is_fighting)
            {
                // BdPackage.BdUtils.log("--- is_fighting");
                if (!is_show_ending)
                {
                    is_show_ending = true;
                    var msg =
                        $"*** 战斗结果: {GameController.instance.fight_ending}, 伤害: {GameController.instance.total_remain_soldier_stars}";
                    BdTools.show_message_box(content: msg);
                    BdPackage.BdUtils.log(msg);
                }
            }

            if (EventManager.instance.animation_event_ls.Count > 0)
            {
                return;
            }

            if (duration_time > parameter.max_time_for_battle)
            {
                if (MyPointsManager.instance.player_comp.curr_atk > 0)
                {
                    if (BaseInfoManager.instance.remain_enemies == 0)
                    {
                        manager.TransitionState(GameStateEnum.Win);
                    }
                    else
                    {
                        manager.TransitionState(GameStateEnum.Store);
                    }
                }
                else
                {
                    manager.TransitionState(GameStateEnum.Lose);
                }
            }
        }

        public void OnExit()
        {
            parameter.battle_objs.SetActive(false);
            // BdPackage.BdUtils.log("BattleState OnExit");

            // GameController.instance.hide_enemy_cards();
            // GameController.instance.clear_store_cards();
            
            GameController.instance.clear_enemy_cards();
            
            // MyPointsManager.instance.add_store_owner(cancel: false);
            MyPointsManager.instance.add_enemy_hero(cancel: true);
        }
    }
    
    
    public class GameExitState : IState
    {
        private GameStateManager manager;
        private GameParams parameter;

        public GameExitState(GameStateManager manager)
        {
            this.manager = manager;
            this.parameter = (GameParams)manager.parameter;
        }
        
        public void OnEnter()
        {
            BdUtils.log("GameExitState OnEnter");
        }

        public void OnUpdate()
        {
            BdUtils.log("GameExitState OnUpdate");
        }

        public void OnExit()
        {
            BdUtils.log("GameExitState OnExit");
        }
    }
    
    public class GameLoseState : IState
    {
        private GameStateManager manager;
        private GameParams parameter;

        public GameLoseState(GameStateManager manager)
        {
            this.manager = manager;
            this.parameter = (GameParams)manager.parameter;
        }
        
        public void OnEnter()
        {
            BdUtils.log("GameLoseState OnEnter");
        }

        public void OnUpdate()
        {
            BdUtils.log("GameLoseState OnUpdate");
        }

        public void OnExit()
        {
            BdUtils.log("GameLoseState OnExit");
        }
    }
    
    public class GameWinState : IState
    {
        private GameStateManager manager;
        private GameParams parameter;

        public GameWinState(GameStateManager manager)
        {
            this.manager = manager;
            this.parameter = (GameParams)manager.parameter;
        }
        
        public void OnEnter()
        {
            BdUtils.log("GameWinState OnEnter");
        }

        public void OnUpdate()
        {
            BdUtils.log("GameWinState OnUpdate");
        }

        public void OnExit()
        {
            BdUtils.log("GameWinState OnExit");
        }
    }
    
}